package com.kaiwukj.android.ufamily.mvp.ui.page.mine.house;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerMineComponent;
import com.kaiwukj.android.ufamily.di.module.MineModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessDeviceResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessRecordResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseMemberResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.MineContract;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.MyHouseMemberAdd)
public class MyHouseMemberAddActivity extends BaseMvpActivity<MyHousePresenter> implements MineContract.HouseView {

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_idcard)
    EditText etIdCard;
    @BindView(R.id.rg_type)
    RadioGroup rgType;

    @Autowired
    int roomId;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @OnClick(R.id.btn_save)
    void onSubmit() {
        String name = etName.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        String idCard = etIdCard.getText().toString().trim();
        int type = 3;
        type = rgType.getCheckedRadioButtonId() == R.id.rb_type_01 ? 3 : 2;
        if (StringUtils.isEmpty(name)) {
            showMessage("姓名不能为空");
            return;
        }
        if (StringUtils.isEmpty(phone) || !RegexUtils.isMobileSimple(phone)) {
            showMessage("请输入正确的手机号");
            return;
        }
        if (StringUtils.isEmpty(idCard) || !RegexUtils.isIDCard18Exact(idCard)) {
            showMessage("请输入正确的身份证号");
            return;
        }
        mPresenter.addMember(name, phone, idCard, type, roomId);
    }


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerMineComponent.builder()
                .appComponent(appComponent)
                .mineModule(new MineModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_house_member_add;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("成员添加");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == MyHousePresenter.ADD) {
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void onFetchHouse(List<MyHouseResult> results) {

    }

    @Override
    public void onFetchHouseMember(List<MyHouseMemberResult> results) {

    }

    @Override
    public void onFetchDoorAccess(DoorAccessDeviceResult result) {

    }

    @Override
    public void onFetchDoorRecord(List<DoorAccessRecordResult> results) {

    }
}
